//
//  YYEUserAccount.swift
//  weibo14
//
//  Created by mac on 16/3/5.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YYEUserAccount: NSObject ,NSCoding{
    

    // 用户的 uid
    var uid:String?
    
    // 过期时间- 秒速
        // expires_in
    var expires_in : Double = 0 {
    
        didSet{
        
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
                                // dateByAddingTimeInterval
        }
    
    }
    
    // 过期的日期
    var expiresDate:NSDate?
    

    // 访问令牌
    var access_token:String?
    
    // 用户的姓名
    var screen_name: String?
    
    // 用户的头像的地址 180X180
    var avatar_large:   String?

    // 过期时间
    // var remind_in: String?
    
    init(dict:[String:AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    // MARK: 归档和解档的方法
    
    func encodeWithCoder(encoder: NSCoder) {
        
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeObject(access_token, forKey: "access_token")
        
        // encoder.encodeObject(expires_in, forKey: "expires_in")
//        encoder.encodeDouble(expires_in, forKey: "expires_in")
        encoder.encodeDouble(expires_in, forKey: "expires_in")
        
        encoder.encodeObject(screen_name, forKey: "screen_name")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeObject(expiresDate, forKey: "expiresDate")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")

    }
    
    required init?(coder decoder: NSCoder) {
        
        uid = decoder.decodeObjectForKey("uid") as? String
        access_token = decoder.decodeObjectForKey("access_token") as? String
        screen_name = decoder.decodeObjectForKey("screen_name") as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
        expires_in = decoder.decodeDoubleForKey("expires_in")
    }
    
     // MARK: - description  方法的重复
    
    // 没有智能提示, 重写类似于 OC 中的 description 的 get方法
    override var description: String {
        let keys = ["uid","expires_in", "access_token"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
